package com.colt.contentcenter.feighclient.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: spring-cloud-alibaba-we-chat-app
 * @description: 传递token的拦截器
 * @author: colt
 * @create: 2020-09-27 10:38
 **/
public class TokenRelayRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //1.获取到token
        //1.从header获取token
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) attributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader("X-Token");
        //2.将token传递  将指定的值设置到header
        if (StringUtils.isNotBlank(token)){
            requestTemplate.header("X-Token",token);
        }

    }
}
